package com.yy.service.front.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.entity.front.ShoppingCart;
import com.yy.entity.front.User;
import com.yy.mapper.front.ShoppingCartMapper;
import com.yy.mapper.front.UserMapper;
import com.yy.service.front.ShoppingCartService;
import com.yy.service.front.UserService;
import com.yy.utils.BaseContext;
import com.yy.utils.MD5Utils;
import com.yy.utils.R;
import com.yy.utils.RegexUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * create by KongFuning on 2022/8/4
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    /*
     * 获取当前用户的购物车列表
     * */
    @Override
    public R cartList() {
        String userId = BaseContext.getCurrentId();
        List<ShoppingCart> cartList = query().eq("user_id", userId).list();
        return R.success(cartList);
    }

    /*
     * 添加到购物车
     * */
    @Override
    public R addToCart(ShoppingCart shoppingCart) {
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setCreateTime(LocalDateTime.now());

        String dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        if(dishId != null){
            // 添加到购物车的是菜品
            qw.eq(ShoppingCart::getDishId,dishId);
        }else{
            // 添加到购物车的是套餐
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        List<ShoppingCart> list = list(qw);

        // 已经存在
        if(!list.isEmpty()){
            // 如果是菜品，查询有没有相同口味的
            if(list.get(0).getDishId() != null){
                for (ShoppingCart one : list) {
                    // 有相同口味，在原来数量基础 +1
                    if(one.getDishFlavor().equals(shoppingCart.getDishFlavor())){
                        one.setNumber(one.getNumber() + 1);
                        updateById(one);
                        return R.success("添加到购物车成功！");
                    }
                }
                // 没有相同口味，直接添加
                save(shoppingCart);
                return R.success("添加到购物车成功！");
            }
            // 如果是套餐，直接在原来数量基础 +1
            ShoppingCart one = list.get(0);
            one.setNumber(one.getNumber() + 1);
            updateById(one);
        }else{
            save(shoppingCart);
        }
        return R.success("添加到购物车成功！");
    }

    /*
     * 清空购物车
     * */
    @Override
    public R deleteCart() {
        String userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(userId != null,ShoppingCart::getUserId,userId);
        remove(qw);
        return R.success("购物车清空成功！");
    }

    @Override
    public R subCart(ShoppingCart shoppingCart) {
        log.info("cart:{}",shoppingCart);
        LambdaUpdateWrapper<ShoppingCart> luw = new LambdaUpdateWrapper<>();
        // 如果要减少的是菜品，需要根据口味来减少
        if(shoppingCart.getDishId() != null){
            luw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            luw.eq(ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
        }else{
            // 减少的是套餐，直接减少
            luw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        // 查询购物车内要减少的菜品或套餐的数量
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(shoppingCart.getDishId() != null,ShoppingCart::getDishId,shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        lqw.eq(shoppingCart.getDishFlavor() != null,ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
        ShoppingCart dishOrSetmeal = getOne(lqw);
        Integer number = dishOrSetmeal.getNumber();

        luw.set(ShoppingCart::getNumber,number - 1);
        update(luw);
        return R.success(shoppingCart);
    }

}
